package org.example.projecttwo.service.Impl;

import org.example.projecttwo.entity.LikeRecord;
import org.example.projecttwo.entity.dto.content.request.LikeDTO;
import org.example.projecttwo.mapper.LikeRecordMapper;
import org.example.projecttwo.service.ILikeRecordService;
import org.example.projecttwo.service.Iuser.IUserStatisticsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author nn
 * @since 2025-11-01
 */
@Service
public class LikeRecordServiceImpl extends ServiceImpl<LikeRecordMapper, LikeRecord> implements ILikeRecordService {

    @Autowired
    private IUserStatisticsService userStatisticsService;

    @Override
    public List<LikeRecord> getUserLikes(Long userId, Integer contentType) {
        QueryWrapper<LikeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (contentType != null) {
            queryWrapper.eq("content_type", contentType);
        }
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<LikeRecord> getContentLikes(Integer contentType, Long contentId) {
        QueryWrapper<LikeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("content_type", contentType)
                .eq("content_id", contentId)
                .orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean checkLike(Long userId, Integer contentType, Long contentId) {
        QueryWrapper<LikeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("content_type", contentType)
                .eq("content_id", contentId);
        return baseMapper.exists(queryWrapper);
    }

    @Transactional
    @Override
    public LikeRecord addLike(Long userId, Integer contentType, Long contentId) {
        // 1. 创建点赞记录
        LikeRecord likeRecord = new LikeRecord();
        likeRecord.setUserId(userId)
                .setContentType(contentType)
                .setContentId(contentId)
                .setCreateTime(LocalDateTime.now());
        baseMapper.insert(likeRecord);

        // 2. 更新用户统计（示例：需要根据内容类型获取作者ID后更新其获赞数）
        // 实际业务中需要从文章/新闻表查询作者ID
        // userStatisticsService.increaseLikeCount(authorId);

        return likeRecord;
    }

    @Transactional
    @Override
    public void cancelLike(Long userId, Integer contentType, Long contentId) {
        // 1. 删除点赞记录
        QueryWrapper<LikeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .eq("content_type", contentType)
                .eq("content_id", contentId);
        baseMapper.delete(queryWrapper);

        // 2. 更新用户统计（示例：需要根据内容类型获取作者ID后减少其获赞数）
        // 实际业务中需要从文章/新闻表查询作者ID
        // userStatisticsService.decreaseLikeCount(authorId);
    }

    @Transactional
    @Override
    public void batchCancelLikes(Long userId, List<LikeDTO> likeList) {
        for (LikeDTO dto : likeList) {
            cancelLike(userId, dto.getContentType(), dto.getContentId());
        }
    }

    @Override
    public int countByContent(Integer contentType, Long contentId) {

        return baseMapper.countByContent(contentType, contentId);
    }
}